<?php
	//sets an error message ($msg) and redirects to $script
	//(requires no html output before) 
	function error_msg($msg, $script = null) {
		$_SESSION['error_msg'] = $msg;

		if (!empty($script)) {
			header('Location: '.$script);			
			exit();
		}
	}
	
	//sets a notice message ($msg) 
	// and redirects to $script (if not empty)
	//(requires no html output before) 
	function notice_msg($msg, $script = null) {
		$_SESSION['notice_msg'] = $msg;
		if (!empty($script)) {
  			header('Location: ' . $script);
		  	exit();
		}
	}
	
	//In case of wrong authentication
	//sets login message and redirects to login page
	//(requires no html output before)
	function wrong_auth($msg) {
        include('site/include/utils_site.php');
        set_language();
        if (stripos($msg, "LANG_") === false);
        else
            $msg = constant($msg);
        
		unset($_SESSION['user_logat']);
		error_msg($msg, 'site_login.php');
	}
	
	function gen_md5_salt($saltgen) {
		$salt = '$1$';
		while (strlen($salt)<12) $salt.=$saltgen;
		return $salt;
	}
	
	//crypts a password using username for salt generation
	function crypt_password($username, $password) {
	  if (!isset($username) || !isset($password))
	    return null;
	    
	  // Get the crypt salt from the username
	  $salt = gen_md5_salt($username);
	  // Encrypt the password
	  return crypt($password, $salt);
	  
	}
	
	//generates a random password
	function createRandomPassword() {
	  $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
	  srand((double)microtime()*1000000);
	  $i = 0;
	  $pass = '' ;
	  while ($i < 8) {
	    $num = rand() % 36;
	    $tmp = substr($chars, $num, 1);
	    $pass = $pass . $tmp;
	    $i++;
	  }
	  return $pass;
	}
	
	//check if the logged user has the permission $perm
	function has_permission($perm) {
		if (empty($_SESSION['user_logat'])) return false;
		return (($_SESSION['user_logat']->tip & $perm) == $perm);
	}
	
	/*
	 * checks if the current authenticated user has the required permissions 
	 * and also check is the clietn is rau_platnic and adds the corresponding messages
	 * if not requires relogin
	 * We can use this function even without the $perm parameter. When we do that we only check
	 * if the client is rau platnic 
	 */
	
	function check_permissions($perm = null, $page = null) {
		if (empty($_SESSION['user_logat']) || !($_SESSION['user_logat'] instanceof User)) {			
            wrong_auth("LANG_Have_to_be_logged_in");
		}
		
		if (isset($perm) && !has_permission($perm)) {
            error_msg(LANG_Wrong_User_Permission, "home.php");
		}

		if ($_SESSION['client_logat']->rau_platnic == CT_RAU_PLATNIC_BLOCK) {

			if ($page != 'stari_plati.php' && $page != 'contact.php') {
				error_msg("LANG_Trebuie_sa_platiti", "stari_plati.php");
			}
			else {
				error_msg("LANG_Trebuie_sa_platiti");				
			}
		}
		
		if ($_SESSION['client_logat']->rau_platnic == CT_RAU_PLATNIC_PENDING) {
			if ($page != 'stari_plati.php' && $page != 'contact.php') {
				error_msg("LANG_Trebuie_sa_asteptati_validarea_platii", "stari_plati.php");
			}
			else error_msg("LANG_Trebuie_sa_asteptati_validarea_platii");
		}
		
		if ($_SESSION['client_logat']->rau_platnic == CT_RAU_PLATNIC_1) {
			if (has_permission(CT_PERM_ADMIN_ABONAMENT))
				error_msg('LANG_va_apropiati_de_depasire');
			else error_msg('LANG_Va_apropiati_de_depasire_anuntati_admin_pt_plata'); 										
		}
		if ($_SESSION['client_logat']->rau_platnic == CT_RAU_PLATNIC_2) {			
			error_msg('LANG_dupa_15_zile_inchide_robinetul');										
		}						
	}
	
	function isLoggedIn() {
		return isset($_SESSION['user_logat']);
	}
	
	function checkIsLoggedIn() {
		if (empty($_SESSION['user_logat']) || !($_SESSION['user_logat'] instanceof User)) {
			wrong_auth(LANG_Have_to_be_logged_in);
		}
	}

?>